package com.bw.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.bw.domain.SysUser;
import com.bw.service.SysUserService;

import com.bw.utils.common.JWTUtils;
import com.bw.utils.common.Result;
import com.bw.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.jnlp.UnavailableServiceException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author bobo棒
 * @date 2023年10月24日 15:46
 */

@RequestMapping("auth")
@RestController
public class AuthController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    RedisTemplate redisTemplate;
/*    @Resource
    AuthenticationManager authenticationManager;*/

    public static final String salt="123456";

    @RequestMapping("login")
    public Result login(@RequestBody UserVO userVO){
        //1.先判断验证码
        String codeRedis = (String) redisTemplate.opsForValue().get(userVO.getCodeKey());
        if(!userVO.getCode().equals(codeRedis)){
            return Result.fail("验证码错误");
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,userVO.getUsername());

        SysUser dbUser = sysUserService.getOne(queryWrapper);
        if(null ==dbUser){
              return Result.fail("无此用户");
        }

        //使用md5加盐加密
        String s = DigestUtil.md5Hex(userVO.getPassword() + salt);
        if(!s.equals(dbUser.getPassword())){
            return Result.fail("密码错误");
        }

/*
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userVO.getUsername(), userVO.getPassword()+salt);
        authenticationManager.authenticate(authenticationToken);//*/
        //登录成功,生成token
        String token = JWTUtils.createToken(userVO.getUsername(), 60 * 12);

        return Result.success(token);
    }


    @RequestMapping("getCodeImg")
    public void getCodeImg(String codeKey, HttpServletResponse response) throws IOException {
//定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        captcha.write(response.getOutputStream());//1. 输出给调用者
        redisTemplate.opsForValue().set(codeKey,captcha.getCode(),5, TimeUnit.MINUTES);//2  验证码后端存入redis,并设置过期时间为5分钟
    }

   /* @Resource
    PasswordEncoder passwordEncoder;*/

    //密码初始化
    @RequestMapping("initPWD")
   // @PostConstruct
    public String initPWD(){
      // return passwordEncoder.encode("123456"+salt);
        return DigestUtil.md5Hex("123456"+salt);
    }
}
